﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Entities;
using System.Collections.Specialized;
using MAF.CRUD;
using System.Security.Principal;
using MAF.Common;

namespace Domain.Collections
{
    using Domain.Collections.Impl;

    /// <summary>
    /// 内存中的数据库
    /// </summary>
    public static class DbCollections
    {
        // 新闻
        public static ICRUD<CategoryInfo> NewsCategoryCollection;
        public static ICRUD<Content> NewsCollection;

        // 知识库
        public static ICRUD<CategoryInfo> KnowledgeCategoryCollection;
        public static ICRUD<Content> KnowledgeCollection;

        // 公司基础信息管理
        public static ICRUD<CategoryInfo> CompanyCategoryCollection;
        public static ICRUD<Content> CompanyCollection;

        // 吉瑞芬公司产品
        public static ICRUD<CategoryInfo> TriliphCategoryCollection;
        public static ICRUD<ProductInfo> TriliphCollection;

        // 纽恩健品牌产品
        public static ICRUD<CategoryInfo> NuenJaneCategoryCollection;
        public static ICRUD<ProductInfo> NuenJaneCollection;

        // 经销商
        public static ICRUD<DealerInfo> DealerCollection;

        // 留言
        public static ICRUD<MessageInfo> MessageCollection;

        static DbCollections()
        {
            // 新闻
            NewsCategoryCollection = new DbCollection<CategoryInfo>();
            NewsCollection = new DbCollection<Content>();

            // 知识库
            KnowledgeCategoryCollection = new DbCollection<CategoryInfo>();
            KnowledgeCollection = new DbCollection<Content>();

            // 公司基础信息管理
            CompanyCategoryCollection = new DbCollection<CategoryInfo>();
            CompanyCollection = new DbCollection<Content>();

            // 吉瑞芬公司产品
            TriliphCategoryCollection = new DbCollection<CategoryInfo>();
            TriliphCollection = new DbCollection<ProductInfo>();

            // 纽恩健品牌产品
            NuenJaneCategoryCollection = new DbCollection<CategoryInfo>();
            NuenJaneCollection = new DbCollection<ProductInfo>();

            // 经销商
            DealerCollection = new DbCollection<DealerInfo>();

            // 留言
            MessageCollection = new DbCollection<MessageInfo>();
        }
    }
}

namespace Domain.Collections.Impl
{
    public class DbCollection<T> : ICRUD<T> where T : InfoBase
    {
        Dictionary<Guid, T> db = new Dictionary<Guid, T>();

        #region IDbCollection<CategoryInfo> 成员

        public void Add(T info)
        {
            db.Add(info.Id, info);
        }

        public void Update(T info)
        {
            if (db.ContainsKey(info.Id))
                db[info.Id] = info;
            else
                throw new Exception("没有找到要修改的对象");
        }

        public int Delete(string ids)
        {
            string[] ss = ids.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in ss)
            {
                db.Remove(new Guid(s));
            }
            return ss.Length;
        }

        public T Show(Guid id)
        {
            if (!db.ContainsKey(id))
                return default(T);

            return db[id];
        }

        public IList<T> Index(BaseQueryVo vo)
        {
            T[] arry = db.Values.ToArray<T>();
            List<T> list = new List<T>();
            list.AddRange(arry);
            return list;
        }

        public int Count(BaseQueryVo vo)
        {
            return db.Count();
        }

        public BaseQueryVo ParseToQueryVo(NameValueCollection nvc)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
